﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using MidimediaFinder.Resources;
using System.Windows.Media.Imaging;

namespace MidimediaFinder
{
    public partial class ItemPage : PhoneApplicationPage
    {
        public static item SelectedItem;
        // Constructor
        public ItemPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            Comments_List.DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(ItemPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void ItemPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            if (SelectedItem == null)
            {
                NavigationService.GoBack();
            }
            
            SetElements();
        }
        
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            try
            {
                var show_comments = NavigationContext.QueryString["show_comments"] == "1" ||
                    NavigationContext.QueryString["show_comments"] == "";
                if (!show_comments)
                //PivotControl.Items.Remove(Pivot_Comments);
                {
                    EditComment_Button.Visibility = System.Windows.Visibility.Collapsed;
                    EditComment_Grid.RowDefinitions[0].Height = new GridLength(0);
                }
            }
            catch (KeyNotFoundException ex)
            {
                ex.ToString();
            }
        }

        private void SetElements()
        {
            TypeArea.Text = SelectedItem.Type_Name;
            NameArea.Text = SelectedItem.Name;
            
            EmailArea.Text = SelectedItem.Email;
            AddressArea.Text = SelectedItem.Adress;

            if (SelectedItem.PhoneNumber != "")
            {
                PhoneArea.Text = SelectedItem.PhoneNumber;
            }
            else
            {
                PhoneArea.Visibility = System.Windows.Visibility.Collapsed;
                PhoneButton.Visibility = System.Windows.Visibility.Collapsed;
                PhoneImg.Visibility = System.Windows.Visibility.Collapsed;
                PhoneLabel.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (SelectedItem.Description != "")
            {
                DescriptionArea.Text = SelectedItem.Description;
            }
            else
            {
                DescriptionArea.Visibility = System.Windows.Visibility.Collapsed;
                DescriptionLabel.Visibility = System.Windows.Visibility.Collapsed;
            }


            if (SelectedItem.Website != "")
            {
                WebsiteArea.Text = SelectedItem.Website;
            }
            else
            {
                WebsiteArea.Visibility = System.Windows.Visibility.Collapsed;
                WebsiteButton.Visibility = System.Windows.Visibility.Collapsed;
                WebsiteImg.Visibility = System.Windows.Visibility.Collapsed;
                WebsiteLabel.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (SelectedItem.FBUrl != "")
            {
                FBArea.Text = SelectedItem.FBName;
            }
            else
            {
                FBArea.Visibility = System.Windows.Visibility.Collapsed;
                FBButton.Visibility = System.Windows.Visibility.Collapsed;
                FBImg.Visibility = System.Windows.Visibility.Collapsed;
                FBLabel.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (SelectedItem.TwitterUrl != "")
            {
                TwitterArea.Text = SelectedItem.TwitterName;
            }
            else
            {
                TwitterArea.Visibility = System.Windows.Visibility.Collapsed;
                TwitterButton.Visibility = System.Windows.Visibility.Collapsed;
                TwitterImg.Visibility = System.Windows.Visibility.Collapsed;
                TwitterLabel.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (SelectedItem.RegisteredOn != "")
            {
                RegisteredOnArea.Text = SelectedItem.RegisteredOn;
            }
            else
            {
                RegisteredOnArea.Visibility = System.Windows.Visibility.Collapsed;
                RegisteredOnImg.Visibility = System.Windows.Visibility.Collapsed;
                RegisteredOnLabel.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (SelectedItem.Promotions != null && SelectedItem.Promotions.Count > 0)
            {
                App.ViewModel.SelectedPromotions.Clear();
                for (int i = 0; i < SelectedItem.Promotions.Count; i++)
                {
                    var promotion = SelectedItem.Promotions[i];
                    if(!promotion.ImageUrl.Contains(MainPage.hostname))
                        promotion.ImageUrl = MainPage.hostname + promotion.ImageUrl;
                    if (!promotion.Icon.Contains(MainPage.hostname))
                        promotion.Icon = MainPage.hostname + promotion.Icon;
                    var entry = new PromotionViewModel()
                    {
                        _Name = promotion.Short_description,
                        _Distance = promotion.Distance + " KM",
                        _ImageUrl = new Uri(promotion.ImageUrl, UriKind.RelativeOrAbsolute),
                        _Item = promotion,
                        _Hint = promotion.Hint,
                        _Icon = new Uri(promotion.Icon, UriKind.RelativeOrAbsolute),
                    };
                    App.ViewModel.SelectedPromotions.Add(entry);
                }
            }
            else
                PivotControl.Items.Remove(Pivot_Promotions);

            if (SelectedItem.AllowComments == "1" )
            {
                if (SelectedItem.Comments == null)
                    SelectedItem.Comments = new List<comment>();
                App.ViewModel.Comments.Clear();
                var plain_star = new Uri("\\star.png", UriKind.Relative);
                for (int i = 0; i < SelectedItem.Comments.Count; i++)
                {
                    var c = SelectedItem.Comments[i];
                    var entry = new CommentViewModel()
                    {
                        _Username = c.Username,
                        _Comment = c.Comment,
                        _Datetime = c.Datetime
                    };

                    if (c.Stars >= 5)
                        entry._Star5 = plain_star;


                    if (c.Stars >= 4)
                        entry._Star4 = plain_star;


                    if (c.Stars >= 3)
                        entry._Star3 = plain_star;


                    if (c.Stars >= 2)
                        entry._Star2 = plain_star;


                    if (c.Stars >= 1)
                        entry._Star1 = plain_star;


                    App.ViewModel.Comments.Add(entry);
                }
            }
            else
                PivotControl.Items.Remove(Pivot_Comments);
            


            if (SelectedItem.Images != null && SelectedItem.Images.Count > 0)
            {
                App.ViewModel.Images.Clear();
                for (int i = 0; i < SelectedItem.Images.Count; i++)
                {
                    var promotion = SelectedItem.Images[i];
                    if (!promotion.Contains(MainPage.hostname))
                        promotion = MainPage.hostname + promotion;
                    var entry = new ImageViewModel()
                    {
                        _ImageUrl = new Uri(promotion, UriKind.RelativeOrAbsolute)
                    };
                    App.ViewModel.Images.Add(entry);
                }
                Images_List.Visibility = System.Windows.Visibility.Visible;
                ImageUrl.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if(SelectedItem.ImageUrl!="")
            {
                Images_List.Visibility = System.Windows.Visibility.Collapsed;
                if (!SelectedItem.ImageUrl.Contains(MainPage.hostname))
                    SelectedItem.ImageUrl = MainPage.hostname + SelectedItem.ImageUrl;
                ImageUrl.Source = new BitmapImage(new Uri(SelectedItem.ImageUrl, UriKind.RelativeOrAbsolute));
            }
            else
                PivotControl.Items.Remove(Pivot_Images);

            if (SelectedItem.MyComment != null)
            {
                EditComment_Button.Content = Strings.EditComment;
            }
            else
                EditComment_Button.Content = Strings.SendComment;
        }

        private void EmailButton_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask etc = new EmailComposeTask();
            etc.Subject = Strings.Title;
            etc.To = SelectedItem.Email;
            etc.Show();
        }

        private void PhoneButton_Click(object sender, RoutedEventArgs e)
        {
            PhoneCallTask pct = new PhoneCallTask();
            pct.PhoneNumber = SelectedItem.PhoneNumber;
            pct.DisplayName = SelectedItem.Name;
            pct.Show();
        }

        private void FBButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.Uri = new Uri(SelectedItem.FBUrl, UriKind.RelativeOrAbsolute);
            wbt.Show();
        }

        private void TwitterButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.Uri = new Uri(SelectedItem.TwitterUrl, UriKind.RelativeOrAbsolute);
            wbt.Show();
        }

        private void MapButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.Uri = new Uri(SelectedItem.Map, UriKind.RelativeOrAbsolute);
            wbt.Show();
        }

        private void WebsiteButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.Uri = new Uri(SelectedItem.Website, UriKind.RelativeOrAbsolute);
            wbt.Show();
        }

        private void Promotions_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = Promotions_List.SelectedItem as PromotionViewModel;
            if (item == null || item._Item == null)
                return;
            PromotionPage.SelectedItem = item._Item;
            Promotions_List.SelectedIndex = -1;
            NavigationService.Navigate(new Uri("/PromotionPage.xaml", UriKind.Relative));
        }

        private void Comments_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Comments_List.SelectedIndex = -1;
        }

        private void Images_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Images_List.SelectedIndex = -1;
        }

        private void EditComment_Button_Click(object sender, RoutedEventArgs e)
        {
            EditCommentPage.SelectedItem = SelectedItem;
            NavigationService.Navigate(new Uri("/EditCommentPage.xaml", UriKind.Relative));
        }

        private void ShareEMailButton_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask etc = new EmailComposeTask();
            etc.Body = Storage.ComposePoiEmail(SelectedItem);
            etc.Subject = Strings.Title + SelectedItem.Name;
            etc.Show();
        }

        private void ShareSMsButton_Click(object sender, RoutedEventArgs e)
        {
            SmsComposeTask etc = new SmsComposeTask();
            etc.Body = Storage.ComposePoiSMS(SelectedItem);
            etc.Show();
        }
    }
}